package com.lora.util;

import com.lora.common.ColumnParse;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.Map;

/**
 *
 * 功能描述:
 * class处理工具类
 * @param:
 * @return:
 * @auther: lwt
 * @date: 2019/6/6 0006 下午 4:23
 */
public class ClassFormatUtil {
    /**
     *
     * 功能描述:
     * 对象赋值
     * @param:
     * @return:
     * @auther: lwt
     * @date: 2019/6/6 0006 下午 4:17
     */
    public static Object setFiledValue(Map<String,String> map,Object bean){
        map.forEach((String str,String o)->{
            try {
                Field declaredField = bean.getClass().getDeclaredField(str);
                declaredField.setAccessible(true);
                ColumnParse annotation = declaredField.getAnnotation(ColumnParse.class);
                if(annotation!=null){
                    double ratio = annotation.ratio();
                    double value = Integer.parseInt(o) * ratio;
                    DecimalFormat df = new DecimalFormat("#.00");
                    o=df.format(value);
                }
                declaredField.set(bean,o);
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        });
        return bean;
    }
}
